package common;

import interfaces.Model;

import java.util.LinkedList;
import java.util.List;
import java.util.Properties;

import dao.PropertyIO;

public class TestController {
	private static final String propertyFilename = "conf/test.config";
	public static final String EXIT = "exit";

	public void proceed(String testFile) {
		Properties properties = PropertyIO.load(propertyFilename);
		ModelComposite model = new ModelComposite();
		model.initialize(properties);

		model.read(testFile);
		interaction(model, getInput(properties));

	}

	protected void interaction(Model model, InputWrapper input) {
		List<String> history = new LinkedList<String>();

		while (true) {
			String line = input.next();

			if (line == null || line.isEmpty() || line.compareTo(EXIT) == 0) {
				break;
			}
			
			List<Double> distribution = model.test(history);
			System.out.println(getDistribution(distribution));

			history.add(line);
		}
	}
	
	protected InputWrapper getInput(Properties properties) {
		String inputType = properties.getProperty("input_type");
		InputWrapper input;
		if(inputType.compareTo("console") == 0) {
			input = new InputWrapper(); 
		} else if (inputType.compareTo("batch") == 0) {
			input = new BatchedInputWrapper();
		} else {
			throw new RuntimeException("input type not defined!");
		}
		input.initialize(properties);
		return input;
	}

	protected String getDistribution(List<Double> list) {
		StringBuilder sb = new StringBuilder();
		for (double value : list) {
			sb.append(value);
			sb.append("\t");
		}
		return sb.toString().trim();
	}

	public static void main(String[] args) {
		if (args.length != 1) {
			System.out.println("USAGE: modelA.dat");
			return;
		}

		TestController controller = new TestController();
		controller.proceed(args[0]);
	}
}
